package animator.phantom.controller;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.renderer.ImageOperation;
import animator.phantom.renderer.param.*;
import animator.phantom.gui.timeline.TimeLineSliders;
import animator.phantom.gui.AnimFrameGUIParams;

//--- Layer for updates that concern multiple GUI components.
public class UpdateController
{
	//--- Value change update sources.
	public static final int PARAM_EDIT = 1;
	public static final int VIEW_EDIT = 2;
	public static final int KF_EDIT = 3;

	private static ImageOperation targetIOP = null;

	//--- Theres only one iop that is target of editing.
	public static void editTargetIOPChangedFromClipEditor( ImageOperation iop )
	{
		EditorsController.initKeyFrameEditor( iop );
		GUIComponents.renderFlowPanel.setAsOnlySelected( iop );
		EditorsController.setEditorLayerForIop( iop );
		targetIOP = iop;
	}
	//--- Theres only one iop that is target of editing. This is called from flow editor.
	public static void editTargetIOPChanged( ImageOperation iop )
	{
		EditorsController.initKeyFrameEditor( iop );
		TimeLineController.targetIopChanged( iop );
		EditorsController.setEditorLayerForIop( iop );
		targetIOP = iop;
	}
	//--- Theres only one iop that is target of editing. This is called from view editor.
	public static void editTargetIOPChangedFromViewEditor( ImageOperation iop )
	{
		EditorsController.initKeyFrameEditor( iop );
		TimeLineController.targetIopChanged( iop );
		GUIComponents.renderFlowPanel.setAsOnlySelected( iop );
		targetIOP = iop;
	}
	//--- Theres only one iop that is target of editing. This is called from Stack editor.
	//--- Stack iops are not part of timeline or flow and cannot edited in those editors
	public static void editTargetIOPChangedFromStackEditor( ImageOperation iop )
	{
		EditorsController.initKeyFrameEditor( iop );
		EditorsController.setEditorLayerForIop( null );
		TimeLineController.targetIopChanged( null );
		GUIComponents.renderFlowPanel.setAsOnlySelected( null );
		targetIOP = iop;
	}
	//--- Called by for eg. PreViewPanel	
	public static ImageOperation getTargetIOP(){ return targetIOP; }	
	//--- Called after param value changed, without source.
	public static void valueChangeUpdate()
	{
		valueChangeUpdate( -1, false );
	}
	//--- Called after param value changed, with specified source.
	public static void valueChangeUpdate( int source )
	{
		valueChangeUpdate( source, false );
	}
	//--- Called after param value changed during drag, with specified source.
	public static void valueChangeUpdate( int source, boolean dragInProgress )
	{
		if( source != PARAM_EDIT ) ParamEditController.updateEditFrame();
		if( source != KF_EDIT ) EditorsController.updateKFForValueChange();
		EditorsController.displayCurrentInViewEditor( dragInProgress );
	}
	//--- Called after current frame changed.
	//--- All editor which have their values 
	//--- Less gui is updated if mouse still pressed to make make dragging
	//--- frame pointer in in timeline faster.
	//--- Timeline update handled separately..
	public static void updateCurrentFrameDisplayers( boolean dragInProgress )
	{
		ParamEditController.updateEditFrame();
		PreviewController.currentFrameChanged();
		EditorsController.displayCurrentInViewEditor( dragInProgress );
	}
	//--- Update so that timeline dragging showes movement, always when dragging.
	public static void updateMovementDisplayers() 
	{
		PreviewController.currentFrameChanged();
		EditorsController.displayCurrentInViewEditor( true );
	}
	//--- Updates all gui that is changed when timeline scale changes.
	public static void updateTimeLineScale( int scale, TimeLineSliders source )
	{
		//--- Set scale and update clip editor gui and sliders in KFEditor
		TimeLineController.setTimeLineScale( scale, source );
		//--- Set position so that current frame in middle if possible
		//--- Get view width in panels and other info
		float pixelsPerFrame = TimeLineController.getCurrentScaleMultiplier();
		int framesInPanel = 
			Math.round( AnimFrameGUIParams.getTimeEditRightColWidth() / pixelsPerFrame );
		int current = TimeLineController.getCurrentFrame();
		int framesInMovie = ProjectController.getLength();
		int lastPos = (framesInMovie - framesInPanel);
		//--- if cannot be centered because current too early set pos 0
		if( current < framesInPanel / 2 )
			TimeLineController.setTimeLinePosition( 0, source );
		//--- if cannot be centered because current too late set pos last pos		
		else if( current > lastPos )
			TimeLineController.setTimeLinePosition(lastPos, source );
		//--- center
		else
			TimeLineController.setTimeLinePosition( current - ( framesInPanel / 2 ), source );
		
		//--- Update gui here.
		EditorsController.updateScaleOrPos();
		if( source != null )
			source.calibratePositionSlider();
		else //--- Update from keyboard shortcut
		{
			int slidersIndex = GUIComponents.animatorFrame.getSelectedEditorTab() - 1;
			GUIComponents.sliders.elementAt( slidersIndex ).calibratePositionSlider();
		}
	}
	//--- Updates all gui that is changed when timeLinepositionchanges.
	public static void updateTimeLinePosition( int position, TimeLineSliders source )
	{
		//--- Set scale and update clip editor gui and sliders in KFEditor
		TimeLineController.setTimeLinePosition( position, source );
		EditorsController.updateScaleOrPos();
	}

}//end class
